﻿using Common;
using Common.XmlHandle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SFJDManagement
{
    /// <summary>
    /// Page_PhoneBook.xaml 的交互逻辑
    /// </summary>
    public partial class Page_PhoneBook : Page
    {
        List<ContactPlus> list = new List<ContactPlus>(200);
        WrapPanel header = new WrapPanel();
        DataGrid dataGrid;

        public Page_PhoneBook()
        {
            InitializeComponent();
            Ini();
        }

        private void Ini()
        {
            KeepAlive = false;
            LoadContacts();
            header.HorizontalAlignment = HorizontalAlignment.Center;
            header.Margin = new Thickness(0, 10, 0, 5);
            TextBlock label = new TextBlock()
            {
                Text = "搜索：",
                FontSize = 20
            };
            header.Children.Add(label);
            TextBox searchBox = new TextBox()
            {
                Text = "输入字符进行搜索...",
                Foreground = Brushes.Gray,
                FontSize = 20,
                MinWidth = 500
            };
            searchBox.GotFocus += SearchBox_GotFocus;
            searchBox.LostFocus += SearchBox_LostFocus;
            header.Children.Add(searchBox);
            dataGrid = new DataGrid();
            dataGrid.FontSize = 16;
            dataGrid.ItemsSource = list;
            dataGrid.HeadersVisibility = DataGridHeadersVisibility.Column;
            dataGrid.Margin = new Thickness(0, 50, 0, 5);
            Grid panel = new Grid()
            {
                HorizontalAlignment = HorizontalAlignment.Center,
            };
            panel.Children.Add(header);
            panel.Children.Add(dataGrid);
            Content = panel;
        }

        private void SearchBox_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox box = sender as TextBox;
            if (box.Text == "")
            {
                box.Text = "输入字符进行搜索...";
                box.Foreground = Brushes.Gray;
            }
        }

        private void SearchBox_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox box = sender as TextBox;
            if(box.Text == "输入字符进行搜索...")
            {
                box.Text = "";
                box.Foreground = Brushes.Black;
            }
        }

        private void LoadContacts()
        {
            List<ClassContact> contacts = Runtime.xmlContact.GetAll();
            foreach(ClassContact contact in contacts)
            {
                list.Add(new ContactPlus(contact));
            }
        }

        public class ContactPlus
        {
            static List<ClassBrigade> list = Runtime.xmlBrigade.GetAll();
            public string 单位 { set; get; }
            public string 姓名 { set; get; }
            public string 电话 { set; get; }
            public ContactPlus(ClassContact contact) {
                单位 = contact.Brigade.ToString();
                foreach(ClassBrigade brigade in list)
                {
                    if(brigade.Guid == contact.Brigade)
                    {
                        单位 = brigade.FullName;
                        break;
                    }
                }
                姓名 = contact.Name;
                电话 = contact.Phone;
            }
        }
    }
}
